from datetime import datetime
from app.extensions import db

class Student(db.Model):
    """学生表模型"""
    __tablename__ = 'student'
    
    student_id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='学生唯一标识')
    name = db.Column(db.String(50), nullable=False, comment='学生姓名')
    create_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    stage_tag = db.Column(db.String(20), nullable=True, comment='学习阶段标签')
    parent_id = db.Column(db.Integer, nullable=True, comment='家长用户ID')
    # 逻辑外键字段
    user_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), 
                       nullable=False, comment='学生账号ID')
    
    

    # 关系定义
    tasks = db.relationship('Task', backref='student', lazy='dynamic')
    review_plans = db.relationship('ReviewPlan', backref='student', lazy='dynamic')
    study_records = db.relationship('StudyRecord', backref='student', lazy='dynamic')
    def to_dict(self):
        return {
            'student_id': self.student_id,
            'name': self.name,
            'create_time': self.create_time,
            'stage_tag': self.stage_tag,
            'user_id': self.user_id,
            'parent_id':self.parent_id
        }
    def __repr__(self):
        return f'<Student {self.name}>'